/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.hbzhit.modules.act.controller;

import cn.hutool.core.io.IoUtil;
import com.hbzhit.common.annotation.SysLog;
import com.hbzhit.common.utils.R;
import com.hbzhit.modules.act.enums.ResourceTypeEnum;
import com.hbzhit.modules.act.service.ProcessService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.Map;

/**
 * @author lengleng
 * @date 2018/9/25
 */
@Api(tags = {"流程定义接口"})
@RestController
@AllArgsConstructor
@RequestMapping("/act/process")
public class ProcessController {
    private final ProcessService processService;

    @ApiOperation(value = "流程定义分页数据")
    @GetMapping
    public R list(@RequestParam Map<String, Object> params) {
        return R.ok(processService.getProcessByPage(params));
    }

    @ApiOperation(value = "导入BPMN文件")
    @SysLog("导入BPMN文件")
    @PostMapping({"/deploy"})
    public R deploy(@RequestParam(value = "file", required = false) MultipartFile file) {
        if (processService.importBPMN(file)) {
            return R.ok();
        }

        return R.error("部署流程失败！");
    }

    @ApiOperation(value = "查看流程图或XML数据")
    @GetMapping(value = "/resource/{proInsId}/{procDefId}/{resType}")
    public ResponseEntity resourceRead(
            @ApiParam(value = "流程定义ID", required = true) @PathVariable String procDefId,
            @ApiParam(value = "流程实例ID", required = true) @PathVariable String proInsId,
            @ApiParam(value = "数据类型(image/png 或 application/xml)", required = true) @PathVariable String resType
    ) {

        HttpHeaders headers = new HttpHeaders();

        if (ResourceTypeEnum.XML.getType().equals(resType)) {
            headers.setContentType(MediaType.APPLICATION_XML);
        } else {
            headers.setContentType(MediaType.IMAGE_PNG);
        }

        InputStream resourceAsStream = processService.readResource(procDefId, proInsId, resType);
        return new ResponseEntity<>(IoUtil.readBytes(resourceAsStream), headers, HttpStatus.CREATED);
    }

    @ApiOperation(value = "转换为流程模型")
    @GetMapping({"/convert-to-model/{procDefId}"})
    public R convertToModel(@PathVariable("procDefId") String procDefId) {
        if (processService.convertToModel(procDefId)) {
            return R.ok();
        }

        return R.error("转换模型失败！");
    }

    @ApiOperation(value = "删除流程定义")
    @DeleteMapping("/{deploymentId}")
    public R deleteProcIns(@PathVariable String deploymentId) {
        return R.ok(processService.removeProcIns(deploymentId));
    }
}
